#include "stdafx.h"
#include "UnicodeText.h"

using namespace MomogenkyouSystem;

#define DEBUG_UNICODE

// Declare enum type Languages
enum LanguageFlag   
{
	DEFAULT					=	0,				
	TRADITIONAL_CHINESE		=	1,	
	JAPANESE				=	2,
	KOREAN					=	3,
	SIMPLIFIED_CHINESE		=	4
};  

int g_CodePage[5] = {
	0,			// DEFAULT 
	950,		// TRADITIONAL CHINESE
	932,		// JAPANESE
	949,		// KOREAN 
	936			// SIMPLIFIED CHINESE
};

BYTE g_CharSet[5] = {
	DEFAULT_CHARSET,		// Default 
	CHINESEBIG5_CHARSET,	// TRADITIONAL CHINESE
	SHIFTJIS_CHARSET,		// JAPANESE
	HANGUL_CHARSET,			// KOREAN
	GB2312_CHARSET			// SIMPLIFIED CHINESE
};

// Define Default font
char szDefaultFontName[5][19] = {
	"\x54\x00\x61\x00\x68\x00\x6F\x00\x6D\x00\x61\x00\x00\x00",					// Default font
	"\xB0\x65\x30\x7D\x0E\x66\xD4\x9A\x00\x00",									// TRADITIONAL_CHINESE font
	"\x2D\xFF\x33\xFF\x20\x00\x30\xFF\xB4\x30\xB7\x30\xC3\x30\xAF\x30\x00\x00",	// JAPANESE font
	"\x74\xAD\xBC\xB9\x00\x00",													// KOREAN font
	"\xB0\x65\x8B\x5B\x53\x4F\x00\x00"											// SIMPLIFIED_CHINESE font
};

int			UnicodeText::m_nLanguage(0);
BYTE		UnicodeText::m_bComAttr[DEFAULT_BUFFERSIZE];	// The attributes of the composition string
DWORD		UnicodeText::m_dwCompos[DEFAULT_BUFFERSIZE];	//	The Composition
wchar_t		UnicodeText::m_szBuffer[DEFAULT_BUFFERSIZE];	//	Input String Buffer
wchar_t		UnicodeText::m_szComstr[DEFAULT_BUFFERSIZE];	//	Composition  Buffer
wchar_t		UnicodeText::m_szBackup[DEFAULT_BUFFERSIZE];	//	Backup Buffer



bool	UnicodeText::Initialize()
{
	memset(m_szBuffer, 0, sizeof(wchar_t) * DEFAULT_BUFFERSIZE);

	WriteCommandLine("> UnicodeText >> Initialized");
	return true;
}

bool	UnicodeText::Shutdown()
{
	WriteCommandLine("> UnicodeText >> Shutdown");
	return	true;
}

HRESULT	UnicodeText::WndProc(HWND hWnd, const size_t &uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_INPUTLANGCHANGE:
		PostMessage(hWnd, WM_SETINPUTLANG, 0L, 0L);
#ifdef DEBUG_UNICODE
		WriteCommandLine("> UnicodeText : Change Language");
#endif
		break;

	case WM_IME_STARTCOMPOSITION:
#ifdef DEBUG_UNICODE
		WriteCommandLine("> UnicodeText : Start Composition");
#endif
		break;

	case WM_IME_COMPOSITION:
#ifdef DEBUG_UNICODE
		WriteCommandLine("> UnicodeText : Coposing");
#endif
		break;

	case WM_IME_ENDCOMPOSITION:
#ifdef DEBUG_UNICODE
		WriteCommandLine("> UnicodeText : End Composition");
#endif
		break;

	case WM_IME_NOTIFY:
#ifdef DEBUG_UNICODE
		WriteCommandLine("> UnicodeText : Notify");
#endif
		switch(wParam)
		{
		case IMN_OPENCANDIDATE:
			/*if (m_nLanguage == JAPANESE)
						SetCandiDateWindowPos();*/
			break;

		case IMN_SETCONVERSIONMODE:
		case IMN_SETSENTENCEMODE:
			PostMessage(hWnd, WM_RESETMODE, wParam, lParam);
			break;

		case IMN_SETOPENSTATUS:
			PostMessage(hWnd, WM_TOGGLE, wParam, lParam);
			break;
		}
		break;

	case WM_IME_SETCONTEXT:
#ifdef DEBUG_UNICODE
		WriteCommandLine("> UnicodeText : Set Context");
#endif
		/*if ((m_nLanguage ==  JAPANESE) && !m_fIsNT) 				
		{
			PostMessage(hWnd, WM_SETMODE, wParam, lParam);
		}*/
		break;
	}
	return S_OK;
}


void	UnicodeText::SetCandidateWindow()
{
	HIMC			_hIMC;
	//TEXTMETRIC		_TextMetric;
	CANDIDATEFORM	_CandidateForm;
	

	_hIMC	=	ImmGetContext(Window::WindowHandle());
	if(_hIMC == NULL)	return;

	_CandidateForm.dwIndex	=	0;
	_CandidateForm.dwStyle	=	CFS_FORCE_POSITION;

	if(m_nLanguage == JAPANESE)
	{
		// Set candidate window position near editable character
		/*wchar_t *szTmpStr = new(wchar_t[0 + m_nComCursorPos + 1]);

		err = memcpy_s(szTmpStr, WCHARSIZE* (0), m_szBuffer, sizeof(wchar_t) * 0));	 
		ASSERT(!err, "String Error");
		szTmpStr[m_xInsertPos + m_nComCursorPos] = L'\0';
		Candidate.ptCurrentPos.x = X_INIT + GetWidthOfString(szTmpStr);*/
	}
}

std::wstring	UnicodeText::GetComposedString()
{
	return L"NULL";
}